package com.atguigu.gmall.realtime.common.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.lettuce.core.RedisClient;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.async.RedisAsyncCommands;
import org.apache.commons.lang3.StringUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * ClassName: RedisUtil
 * Description:
 * 操作Redis的工具类
 *
 * @Create 2024/4/27 9:06
 */
public class RedisUtil {

    public static JedisPool jedisPool;

    static {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMinIdle(5);
        poolConfig.setMaxTotal(100);
        poolConfig.setMaxIdle(5);
        poolConfig.setBlockWhenExhausted(true);
        poolConfig.setMaxWaitMillis(2000L);
        poolConfig.setTestOnBorrow(true);

        jedisPool = new JedisPool(poolConfig, "hadoop102", 6379, 10000);
    }


    //TODO 获取连接
    //同步
    public static Jedis getJedis() {
        System.out.println("开启Jedis");

        return jedisPool.getResource();
    }

    //异步
    public static StatefulRedisConnection<String ,String> getAsyncRedisConnection() {
        System.out.println("开启Redis异步客户端连接");

        RedisClient redisClient = RedisClient.create("redis://hadoop102:6379/0");
        StatefulRedisConnection<String, String> asyncRedisConn = redisClient.connect();
        return asyncRedisConn;
    }


    //TODO 关闭连接
    //同步
    public static void closeJedis(Jedis jedis) {
        System.out.println("关闭Jedis");

        if (jedis != null) {
            jedis.close();
        }
    }

    //异步
    public static void closeAsyncRedisConnection(StatefulRedisConnection<String, String> asyncRedisConn) {
        System.out.println("关闭Redis异步客户端连接");

        if (asyncRedisConn != null && asyncRedisConn.isOpen()) {
            asyncRedisConn.close();
        }
    }


    //TODO set
    //同步
    public static void writeDim(Jedis jedis, String tableName, String id, JSONObject dimJsonObj) {
        //放数据并设置失效时间
        String redisKey = getRedisKey(tableName, id);
        jedis.setex(redisKey, 24 * 3600, dimJsonObj.toJSONString());
    }

    //异步
    public static void writeDimAsync(StatefulRedisConnection<String, String> asyncRedisConn, String tableName, String id, JSONObject dimJsonObj) {
        String redisKey = getRedisKey(tableName, id);
        //RedisAsyncCommands<String, String> asyncCommands = asyncRedisConn.async();
        asyncRedisConn.async().setex(redisKey, 24 * 3600, dimJsonObj.toJSONString());
    }


    //TODO get
    //同步
    public static JSONObject readDim(Jedis jedis, String tableName, String id) {
        String redisKey = getRedisKey(tableName, id);
        //根据key从Redis获取维度数据
        String dimJsonStr = jedis.get(redisKey);

        //如果不为空，说明缓存命中
        if (StringUtils.isNotEmpty(dimJsonStr)) {
            JSONObject dimJsonObj = JSON.parseObject(dimJsonStr);
            return dimJsonObj;
        }
        return null;
    }

    //异步
    public static JSONObject readDimAsync(StatefulRedisConnection<String, String> asyncRedisConn, String tableName, String id) {
        String redisKey = getRedisKey(tableName, id);
        RedisAsyncCommands<String, String> asyncCommands = asyncRedisConn.async();
        try {
            String dimJsonStr = asyncCommands.get(redisKey).get();
            if (StringUtils.isNotEmpty(dimJsonStr)) {
                JSONObject dimJsonObj = JSON.parseObject(dimJsonStr);
                return dimJsonObj;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return null;
    }

    public static String getRedisKey(String tableName, String id) {
        String redisKey = tableName + ":" + id;
        return redisKey;
    }



}
